package com.allwees.core.dao.configuration;

import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.handlers.MybatisEnumTypeHandler;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.allwees.core.dao.configuration.datapermission.PermissionPaginationInterceptor;
import com.allwees.core.dao.injector.MySqlInjector;
import com.allwees.core.dao.mybatis.handler.MybatisPlusMetaObjectHandler;
import org.apache.ibatis.session.Configuration;
import org.springframework.context.annotation.Bean;

/**
 * MybatisPlus 配置
 *
 * @author yangylsky
 */
@org.springframework.context.annotation.Configuration
public class MybatisPlusAutoConfiguration {
	/**
	 * 分页
	 *
	 * @return
	 */
	@Bean
	public PaginationInterceptor paginationInterceptor() {
		PaginationInterceptor pi = new PermissionPaginationInterceptor();
		pi.setLimit(65535);
		return pi;
	}

	/**
	 * 逻辑SQL注入器（如逻辑删除）
	 *
	 * @return
	 */
	@Bean
	public ISqlInjector sqlInjector() {
		return new MySqlInjector();
	}

	@Bean
	public MybatisPlusMetaObjectHandler mybatisPlusMetaObjectHandler() {
		return new MybatisPlusMetaObjectHandler();
	}

	@Bean
	public ConfigurationCustomizer configurationCustomizer() {
		return new MybatisPlusCustomizers();
	}

	/**
	 * 自定义配置,这是解决枚举类的映射
	 */
    static class MybatisPlusCustomizers implements ConfigurationCustomizer {
		@Override
		public void customize(Configuration configuration) {
			configuration.setDefaultEnumTypeHandler(MybatisEnumTypeHandler.class);
		}
	}
}
